1042C - Array Product - CodeForces Solution


constructive algorithms greedy math *1700

Please click on ads to support us..

C++ Code:

                                       
#include<bits/stdc++.h>
using namespace std;
#define ll long long 
#define ull unsigned long long
#define INF 100000000001
#define pb          emplace_back
#define frr(i,j,k)   for(ll i=j; i<k; i++)
#define all(v) (v).begin(), (v).end()
#define ff       first
#define ss        second
#define el         <<'\n' 
#define M 1000000007
#define mod 998244353
#define pii pair<ll,ll>
#define sz(x) ((int)(x).size())
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define ren         {cout<<"NO"<<enl;}
#define rey         {cout<<"YES"<<endl;}
#define mem1(a)      memset(a, -1 ,sizeof(a));
#define memt(a)      memset(a, true ,sizeof(a));
#define endl          "\n"
#define uniq(x)           x.erase(unique(all(x)),x.end());
 #include <ext/pb_ds/assoc_container.hpp>
 #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
using namespace __gnu_pbds;
 
typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag,
                    tree_order_statistics_node_update>
                    ordered_set;
//---->find parent operation dsu>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
ll find_par(ll u,vector<ll>&parent)
{
  if(u==parent[u])return u;
  return parent[u]=find_par(parent[u],parent);
}
//----------------------------------------------------------------------------------------------
//-->find union operation dsu>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void union1 (ll u,ll v,vector<ll>&parent,vector<ll>&rank)
{
  u= find_par(u,parent);
  v= find_par(v,parent);
  if(rank[u]<rank[v])
  {
    parent[u]=v;
  }else if(rank[v]<rank[u])
  {
    parent[v]=u;
  }else
  {
    parent[v]=u;
    rank[u]++;
  }
}
//---------------------------------------------------------------------------------------------
//--->power function pow(2,3)like something >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
 ll pwr(ll a, ll b)
{
    ll res = 1;
    while (b > 0)
    {
        if (b & 1)
            res = ((res * a));
            //res=res;
        a = (a * a);
        b >>= 1;
    }
    return res;
}
//------------------------------------------------------------------------------------------------
//->modulo multiplication >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
ll mod_mul(ll a, ll b)
{
    a = a % M;
    b = b % M;
    return (((a * b) % M) + M) % M;
}
int ceil_div(int a, int b) { return a % b == 0 ? a / b : a / b + 1; }
//-----------------------------------------------------------------------------------------------
 //->check a no is prime or not >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  bool isprime(ll number)
  {
     if (number < 2)
     {
        return false;
     }
     for (ll i = 2; i * i <= number; ++i)
     {
        if (number % i == 0)
        {
           return false;
        }
     }
     return true;
  }
//-----------------------------------------------------------------------------------------------
  ll p1[1000001];
  ll idx;
void SieveOfEratosthenes(ll n)
{
    // Create a boolean array "prime[0..n]" and initialize
    // all entries it as true. A value in prime[i] will
    // finally be false if i is Not a prime, else true.
    bool prime[n+1];
    memset(prime, true, sizeof(prime));
  
    for (int p=2; p*p<=n; p++)
    {
        // If prime[p] is not changed, then it is a prime
        if (prime[p] == true)
        {
            // Update all multiples of p
            for (int i=p*2; i<=n; i += p)
                prime[i] = false;
        }
    }
  
    // Print all prime numbers
    for (int p=2; p<=n; p++)
    {
       if (prime[p])
       {
          idx++;
        }
      }
}   
//-----------------------------------------------------------------------------------------------
//-> graph related calculations >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
   int dx[] = {-1, 0, 0, 1};
    int dy[] = {0, 1, -1, 0};
   //vector<ll>ar[1000001];
  // vector<ll>ar1[1000001];
    //vector<pair<ll,ll>>ar[1000001];
 // ll dis[10000001]; 
  // ll dis[10000001];
     //ll clr[1000001];
  //  ll dis[10000001];
     //ull par[1000001];
     //ull vis[10000001];
    // ll par[1000001];
   //ll vis[1000001];
    //ll clr[1000001];
//-----------------------------------------------------------------------------------------------
   // set<pii >::iterator it;
//      vector<ll> inv, FactorialInv, Factorial;
// ll beki(ll a, ll b)
// {
//     ll ret = 1 % MOD;
//     a %= MOD;
//     while (b)
//     {
//         if (b & 1LL)
//             ret = ret * a % MOD;
//         a = a * a % MOD;
//         b >>= 1;
//     }
//     return ret;
// }
// void init_combination(ll MAX)
// {
//     Factorial.resize(MAX + 1);
//     FactorialInv.resize(MAX + 1);
//     inv.resize(MAX + 1);
//     Factorial[0] = 1;
//     inv[0] = 1;
//     for (int i = 1; i <= MAX; i++)
//     {
//         Factorial[i] = Factorial[i - 1] * i % MOD;
//     }
//     FactorialInv[MAX] = beki(Factorial[MAX], MOD - 2);
//     for (ll i = MAX - 1; i >= 0; i--)
//     {a
//         FactorialInv[i] = FactorialInv[i + 1] * (i + 1) % MOD;
//     }
//     for (int i = 1; i <= MAX; i++)
//     {
//         inv[i] = FactorialInv[i] * Factorial[i - 1] % MOD;
//     }
// }
// ll combination(ll a, ll b)
// {
//     if ((a == b) || (b == 0))
//     {
//         return 1;
//     }
//     if (a < b)
//         return 0;
//     if (b < 0)
//         return 0;
//     ll   = Factorial[a] * FactorialInv[b] % MOD;
//     ans = ans * FactorialInv[a - b] % MOD;
//     return ans;
// }
void noob()
{
   ll n;
   cin>>n;
   vector<pair<ll,ll>>vp1,vp2,vp3;
   ll cnt_neg=0;
   ll cnt_zero=0;
   ll cnt_pos=0;
   frr(i,1,n+1)
   {
       ll x;cin>>x;
       if(x<0){
           cnt_neg++;
           vp1.push_back({x,i});
       }
       else if(x==0){
           cnt_zero++;
           vp2.push_back({x,i});
       }
       else 
       {
           cnt_pos++;
           vp3.push_back({x,i});
       }
      
   }
   sort(all(vp1));
   reverse(all(vp1));
   if(cnt_neg>0)
   {
        if(cnt_neg%2==0)
        {
              if(cnt_zero>0)
              {
                 ll l1=-1;
                 frr(i,0,vp2.size())
                 {
                     l1= max(l1,vp2[i].ss);
                 }
                 frr(i,0,vp2.size())
                 {
                     if(vp2[i].ss!=l1)
                     {
                         cout<<"1"<<" "<<vp2[i].ss<<" "<<l1<<endl;
                     }
                 }
                 cout<<"2"<<" "<<l1<<endl;
              }
              ll k1=-1;
              ll k2=-1;
              frr(i,0,vp1.size())
              {
                  k1= max(k1,vp1[i].ss);
              }
              frr(i,0,vp3.size())
              {
                  k2= max(k2,vp3[i].ss);
              }
              ll k3= max(k1,k2);
              frr(i,0,vp1.size())
              {
                  if(vp1[i].ss!=k1)
                  {
                      cout<<"1"<<" "<<vp1[i].ss<<" "<<k1<<endl;
                  }
              }
              frr(i,0,vp3.size())
              {
                  if(vp3[i].ss!=k2)
                  {
                      cout<<"1"<<" "<<vp3[i].ss<<" "<<k2<<endl;
                  }
              }
              if(k2!=-1)
              {
                  ll h1= min(k1,k2);
                  cout<<"1"<<" "<<h1<<" "<<k3<<endl;
              }
        }else
        {
             ll l1=-1;
             if(cnt_zero>0)
              {
                 frr(i,0,vp2.size())
                 {
                     l1= max(l1,vp2[i].ss);
                 }
                 frr(i,0,vp2.size())
                 {
                     if(vp2[i].ss!=l1)
                     {
                         cout<<"1"<<" "<<vp2[i].ss<<" "<<l1<<endl;
                     }
                 }
              }
              if(l1>0)
              {
                  ll k1= vp1[0].ss;
                  ll min1= min(l1,k1);
                  ll max1= max(l1,k1);
                  cout<<"1"<<" "<<min1<<" "<<max1<<endl;
                  if(vp3.size()>0|| vp1.size()>1)cout<<"2"<<" "<<max1<<endl;
              }else   
              {
                  cout<<"2"<<" "<<vp1[0].ss<<endl;
              }
              
              ll k1=-1;
              ll k2=-1;
              frr(i,1,vp1.size())
              {
                  k1= max(k1,vp1[i].ss);
              }
              frr(i,0,vp3.size())
              {
                  k2= max(k2,vp3[i].ss);
              }
              ll k3= max(k1,k2);
              frr(i,1,vp1.size())
              {
                  if(vp1[i].ss!=k1)
                  {
                      cout<<"1"<<" "<<vp1[i].ss<<" "<<k1<<endl;
                  }
              }
              frr(i,0,vp3.size())
              {
                  if(vp3[i].ss!=k2)
                  {
                      cout<<"1"<<" "<<vp3[i].ss<<" "<<k2<<endl;
                  }
              }
              if(k2!=-1&& k1!=-1)
              {
                  ll h1= min(k1,k2);
                  cout<<"1"<<" "<<h1<<" "<<k3<<endl;
              }
        }
   }else   
   {
            ll l1=-1;
            frr(i,0,vp2.size())
            {
                l1= max(l1,vp2[i].ss);
            }
            frr(i,0,vp2.size())
            {
                if(vp2[i].ss!=l1)
                {
                    cout<<"1"<<" "<<vp2[i].ss<<" "<<l1<<endl;
                }
            }
            if(l1!=-1)
            {
                if(vp2.size()<n)
                {
                    cout<<"2"<<" "<<l1<<endl;
                }
            }
            ll k1=-1;
            frr(i,0,vp3.size())
            {
                k1= max(k1,vp3[i].ss);
            }
            frr(i,0,vp3.size())
            {
                if(vp3[i].ss!=k1)
                {
                    cout<<"1"<<" "<<vp3[i].ss<<" "<<k1<<endl;
                }
            }
   }
}
int main(){
     
       ll t;
       ios_base::sync_with_stdio(false);
      cin.tie(NULL);
     // ll h=1;
      //SieveOfEratosthenes(100001);
        // cin>>t;
        // while(t--){
        //    noob();
        //  } 
   noob();
}


Comments

Submit
0 Comments
More Questions

1143B - Nirvana
1285A - Mezo Playing Zoma
919B - Perfect Number
894A - QAQ
1551A - Polycarp and Coins
313A - Ilya and Bank Account
1469A - Regular Bracket Sequence
919C - Seat Arrangements
1634A - Reverse and Concatenate
1619C - Wrong Addition
1437A - Marketing Scheme
1473B - String LCM
1374A - Required Remainder
1265E - Beautiful Mirrors
1296A - Array with Odd Sum
1385A - Three Pairwise Maximums
911A - Nearest Minimums
102B - Sum of Digits
707A - Brain's Photos
1331B - Limericks
305B - Continued Fractions
1165B - Polycarp Training
1646C - Factorials and Powers of Two
596A - Wilbur and Swimming Pool
1462B - Last Year's Substring
1608B - Build the Permutation
1505A - Is it rated - 2
169A - Chores
765A - Neverending competitions
1303A - Erasing Zeroes